// pages/group_details/group_details.js
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var mph = require("../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    spell_id_info:[],
    spell_list:[],
    spell_id_res:null,
    userId:"",
    ptpsn:'',
    timer:null,
    ptcont_goods_id:''
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('团信息页面',options);
    var that = this;
    var ptcont = wx.getStorageSync('ptcont');
    var psn = '';
    var type = 1;
    if(ptcont){
      that.setData({
          ptcont:ptcont
      })
       psn = ptcont.psn;
    }
    if (options.goods_id){
      that.setData({
        ptcont_goods_id: options.goods_id
      })
    }
        if (options.psn) {psn = options.psn}
        if(options.type)type = options.type;
        that.spell_id_info(options.psn, type);
        this.spell_list();
  },
  /** 
  * 正在进行的团信息
  */
  spell_id_info:function(psn,type){
    console.log(psn, type);
    var that = this;
    var url = 'index.php?act=other_spell&op=spell_id_info';
    mph.postRequest(url, {
      sn: psn,
      type: type
    },
      function (res) {
        console.log('正在进行的团信息==拼团成功:==>', res);
        if (res.data.code == 200) {
          that.setData({
            spell_id_info: res.data.datas.info,
            spell_id_res: res.data.datas,
            ptcont_goods_id: res.data.datas.my.goods_id
           
          })
          console.log('正在进行的团信息==拼团成功:==>', res.data.datas);
          // console.log('正在进行的团信息:==>',res.data.datas.my.end_time_s);
          let time = res.data.datas.my.end_time_s;
          if (time>0) {
            var t = '';
            that.data.timer=setInterval(function () {
              t = mph.end_Time(time--)
              that.setData({
                end_time: t
              });
            }, 1000)

          }else{
            clearInterval(that.data.timer);
          }
        }
      })
  },  
  /** 
  * 拼团-拼团商品列表
  */
  spell_list:function() {
          let that = this;
         var url = 'index.php?act=other&op=spell_list';
          mph.postRequest(url, {
              page: '1',
              size: '20',
              type: 'center'
          },
            function (res) {
              if (res.data.code == 200) {
                  that.setData({
                    spell_list: res.data.datas.list
                  })
              }
            })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    let cont = this.data.ptcont;
    return {
      title: '参团低价购：' + cont.goods_name,
      imageUrl: cont.groupbuy_image,
      path: '/pages/group_details/group_details?psn=' + cont.psn + '&goods_id=' + cont.goods_id,// 用户点击首先进入的当前页面
      success: function (res) {
          mph.showSuccess('分享成功',1000);
      },
      fail: function (res) {
        mph.showErrorTost()
      }
    }
  },

  cantuan(){
    ///pages/order/order?goodsId={{spell_list.goods_id}}&buynum=1&type=only"
    wx.navigateTo({
      url: "/pages/order/order?goodsId=" + this.data.ptcont_goods_id + "&psn=" + this.data.ptcont.psn +'&buynum=1&type=only'
      // url:"/pages/assemble_product_detail/assemble_product_detail?goods_id={{item.goods_id}}&psn={{item.psn}}"
      ///pages/order/order?goodsId={{spell_list.goods_id}}&psn={{psn}}&buynum=1&type=only"
    })
  },
  go_chanp(){
    wx.navigateTo({
      url: "/pages/assemble_product_detail/assemble_product_detail?goods_id=" + this.data.ptcont.goods_id 
    })
  }
})